<template>
  <div class="system-user-container layout-padding">
    <el-card shadow="hover" class="layout-padding-auto">
      <div class="system-user-search mb15">
        <el-row :gutter="20">
          <!--          <el-col :span="4" class="mb15">-->
          <!--            <el-input size="large" placeholder="请输入应用名称" v-model="appName" style="max-width: 240px"></el-input>-->
          <!--          </el-col>-->
          <!--          <el-col :span="8" class="mb15">-->
          <!--            <el-tree-select-->
          <!--                placeholder="请选择所属教育局"-->
          <!--                v-model="searchState.orgId"-->
          <!--                :data="searchState.sysOrgData"-->
          <!--                :render-after-expand="true"-->
          <!--                style="width: 400px"-->
          <!--                check-strictly-->
          <!--                filterable-->
          <!--                show-all-levels-->
          <!--                multiple-->
          <!--            />-->
          <!--          </el-col>-->
          <!--          <el-col :span="6" class="mb15">-->
          <!--            <el-select v-model="searchState.appSuitCrowds" placeholder="请选择应用适用人群"-->
          <!--                       multiple clearable class="w100">-->
          <!--              <el-option-->
          <!--                  v-for="item in searchState.appSuitCrowdOptions"-->
          <!--                  :key="item.key"-->
          <!--                  :label="item.label"-->
          <!--                  :value="item.value">-->
          <!--              </el-option>-->
          <!--            </el-select>-->
          <!--          </el-col>-->
          <el-col :span="6" class="mb15">
            <!--            <el-button size="large" type="primary" class="ml10" @click="getTableData">-->
            <!--              <el-icon>-->
            <!--                <ele-Search/>-->
            <!--              </el-icon>-->
            <!--              查询-->
            <!--            </el-button>-->
            <el-button size="large" type="primary" class="ml10" @click="getTableData">
              <el-icon>
                <ele-Search/>
              </el-icon>
              查询
            </el-button>

            <el-button size="large" type="success" class="ml10" @click="onOpenAdd('add')">
              <el-icon>
                <ele-FolderAdd/>
              </el-icon>
              新增
            </el-button>
          </el-col>
        </el-row>
      </div>
      <el-table :data="state.tableData.data" v-loading="state.tableData.loading" style="width: 100%">
        <el-table-column type="index" label="序号" width="60"/>
        <el-table-column prop="tenantId" label="租户id" show-overflow-tooltip></el-table-column>
        <el-table-column prop="orgName" label="教育局名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="gmtCreate" label="创建时间" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <el-button :disabled="scope.row.userName === 'admin'" size="small" text type="primary"
                       @click="onOpenEdit('edit', scope.row)">修改
            </el-button>
            <el-button :disabled="scope.row.userName === 'admin'" size="small" text type="primary"
                       @click="onRowDel(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="onHandleSizeChange"
          @current-change="onHandleCurrentChange"
          class="mt15"
          :pager-count="5"
          :page-sizes="[10, 20, 30]"
          v-model:current-page="state.tableData.param.pageNum"
          background
          v-model:page-size="state.tableData.param.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="state.tableData.total"
      >
      </el-pagination>
    </el-card>
    <OrgTenantDialog ref="orgTenantDialogRef" @refresh="getTableData()" :orgTree="searchState.sysOrgData"/>
  </div>
</template>

<script setup lang="ts" name="orgTenant">
import {defineAsyncComponent, reactive, onMounted, ref} from 'vue';
import {ElMessageBox, ElMessage} from 'element-plus';
import {useOrgTenantApi} from '/@/api/orgTenant'
import {systemApi} from "/@/api/system";

const {listByPage, removeById} = useOrgTenantApi();
const {orgTree} = systemApi();
// 引入组件
const OrgTenantDialog = defineAsyncComponent(() => import('/src/views/system/orgTenant/dialog.vue'));
const appName = ref('');
// 定义变量内容
const orgTenantDialogRef = ref();
const state = reactive<OrgTenantState>({
  tableData: {
    data: [],
    total: 0,
    loading: false,
    param: {
      pageNum: 1,
      pageSize: 10,
    },
  },
});

const searchState = ref({})

const loadOrgTreeData = async () => {
  state.tableData.loading = true;
  searchState.value.sysOrgData = [];
  const res = await orgTree()
  if (res.status === 200) {
    searchState.value.sysOrgData = res.data;
    state.tableData.loading = false;
  }
};
// 初始化表格数据
const getTableData = async () => {
  state.tableData.loading = true;
  const data = [];
  const res = await listByPage({
    current: state.tableData.param.pageNum,
    pageSize: state.tableData.param.pageSize,
  });
  if (res.status === 200) {
    // res.data.records.forEach((item: any) => {
    //   if (item.orgId) {
    //     item.orgId = item.orgId.split(',');
    //   }
    // })
    data.push(...res.data.records);
  }
  state.tableData.data = data;
  state.tableData.total = res.data.total;
  state.tableData.loading = false;
};
// 打开新增弹窗
const onOpenAdd = (type: string) => {
  orgTenantDialogRef.value.openDialog(type);
};
// 打开修改弹窗
const onOpenEdit = (type: string, row: RowOrgTenantType) => {
  orgTenantDialogRef.value.openDialog(type, row);
};
// 删除
const onRowDel = (row: RowOrgTenantType) => {
  ElMessageBox.confirm(`确定要删除该租户关系吗？”，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    const res = await removeById({id: row.id})
    if (res.status === 200) {
      await getTableData();
      ElMessage.success('删除成功');
    }
  }).catch(() => {
  });
};
// 分页改变
const onHandleSizeChange = (val: number) => {
  state.tableData.param.pageSize = val;
  getTableData();
};
// 分页改变
const onHandleCurrentChange = (val: number) => {
  state.tableData.param.pageNum = val;
  getTableData();
};
// 页面加载时
onMounted(() => {
  loadOrgTreeData();
  getTableData();
});
</script>

<style scoped lang="scss">
.system-user-container {
  :deep(.el-card__body) {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: auto;

    .el-table {
      flex: 1;
    }
  }
}
</style>
